{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "34d05c8b",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1.函数的定义\n",
    "> 2.函数的使用\n",
    "> 3.函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adff8d67",
   "metadata": {},
   "source": [
    "## 函数的定义\n",
    "* 定义一个函数的一般步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "efa3f1f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a, end=' ') # 0  1  1 2 ...\n",
    "    a, b = b, a+b # a = 1  a = 1  b = 2  a = 2 b = 3\n",
    "print()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e878da6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# .....我想重复使用某一个功能（避免重复造轮子）\n",
    "#......使用的人可以不懂代码，只要会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "15a98097",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a, end=' ') # 0  1  1 2 ...\n",
    "        a, b = b, a+b # a = 1  a = 1  b = 2  a = 2 b = 3\n",
    "    print()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8480f4ef",
   "metadata": {},
   "source": [
    "函数的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dc3bc864",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59c2cc2f",
   "metadata": {},
   "source": [
    ">1.定义函数的一般方法：\n",
    "***\n",
    "def function_name(): 1.function_name遵循变量定义的规则 区别数学函数==>f（x）pythion的函数可以自定义名称\n",
    "\"\"\"函数的注释\"\"\"    2.函数的注释可以和使用者交流\n",
    "  函数体        3.函数体要和定义函数保持一定tab缩进\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "64c76475",
   "metadata": {},
   "outputs": [],
   "source": [
    "#写一个函数实现hello word\n",
    "#写一个函数实现hello usename\n",
    "def hello(): \n",
    "    \"\"\"打印你好\"\"\"\n",
    "    print(\"hello world\")\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "09781b02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "91cb8bdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello_user(username):\n",
    "    \"\"\"打印，你好，xxx同学~\"\"\"\n",
    "    print(\"你好，\"+ username.title()+\"同学~\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f945e481",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，Jack同学~\n"
     ]
    }
   ],
   "source": [
    "hello_user(\"jack\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efe508f7",
   "metadata": {},
   "source": [
    "## ３．函数的参数\n",
    ">1.形式参数：\n",
    ">>1.定义函数时，写在 function_name 括号里面的参数\n",
    ">>2.在定义函数时，形式参数不占用空间\n",
    ">２．实际参数：\n",
    ">>1.调用函数时，写在function_name括号里面的参数\n",
    ">>２．在使用函数时，形式参数被分配空间，变为实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b9035503",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name): # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "dd8c9aef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆豆\") # \"dog\",\"豆豆\" 实际参数 实参"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "602323dc",
   "metadata": {},
   "source": [
    ">当函数具有多个参数时\n",
    ">>位置参数\n",
    "形式参数有先后顺序，函数调用过程中应考虑参数的先后顺序，避免出现顺序错误的笑话\n",
    "调用函数过程中要考虑形式参数的个数\n",
    ">>关键字参数\n",
    "调用函数时，通过赋值的方式传递给定义好的形参变量数据\n",
    "关键字参数不考虑先后顺序，因为是指定变量赋值\n",
    ">>默认参数\n",
    "在形式参数中，提前预先设置一个用户可能使用的参数值\n",
    "用户在使用/调用函数时，可以省略不给默认参数赋值\n",
    "用户给默认参数赋值，将替换默认参数的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6e75aee0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name): # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "abcc9eb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 在使用 describe_pet() 函数时 需要考虑 参数的位置，从而进行参数数据的输入（实参的调用）\n",
    "# 位置参数：\n",
    "describe_pet(\"dog\",\"豆豆\") #　要根据　形式参数的位置先后顺序　先写animal_type，再写pet_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "593ca016",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数使用\n",
    "describe_pet(pet_name=\"豆豆\",animal_type=\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9401b422",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数\n",
    "def describe_pet(pet_name,animal_type=\"dog\"): # animal_type,pet_name 是形式参数 形参\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e4ece72e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60f1224f",
   "metadata": {},
   "source": [
    " ## 参数使用过程中的思考与问题\n",
    " >1. 思考：关键字参数和位置参数是否可以共同使用？\n",
    " >> 1.位置参数均要放在关键字参数前面\n",
    " >>2.映射，不能出现多重值\n",
    ">2.定义函数：默认参数和非默认参数的先后关系\n",
    ">>1.非默认参数要在默认参数的前面\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "908a3de6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def text1(x,y,z):\n",
    "    \"\"\"打印xyz的值\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "5929b530",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-54-c196647527b4>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-54-c196647527b4>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    text1(1，4，z=3)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "text1(1，4，z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3318387e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#案例2\n",
    "def text1(x,y,z=3):\n",
    "    \"\"\"打印xyz的值\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "999f21f8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
